package com.hw.protocol.tool.controller.transform;

import com.hw.protocol.tool.config.HbProtocolEnum;
import com.hw.protocol.tool.decode.hb.*;
import com.hw.protocol.tool.model.entity.HbProtocol;
import com.hw.protocol.tool.model.entity.MeterData;
import com.hw.protocol.tool.model.entity.hb.*;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * @author : lihy
 * @date : 2024/3/14 15:39
 * description:
 */
@Slf4j
public class HbTransform extends AbsTransform {
    public static String transform(Object data) {
        HbProtocol protocol = (HbProtocol)data;
        List<MeterData> meterDataList = new LinkedList<>();
        switch (Objects.requireNonNull(HbProtocolEnum.getProtocol(protocol.getObjId()))) {
            case FLOW_UPLOAD:
                Flower flower = new FlowerDecode(protocol.getDevSerial(), protocol.getData()).buildObject();
                MeterData mdFlow = HbTransformUtil.transform(flower);
                meterDataList.add(mdFlow);
                break;
            case BOX_UPLOAD:
                RegulatorBox box = new RegulatorBoxDecode(protocol.getDevSerial(), protocol.getData()).buildObject();
                MeterData mdBox = HbTransformUtil.transform(box);
                meterDataList.add(mdBox);
                break;
            case LEAKAGE_UPLOAD:
                Leakage leakage = new LeakageDecode(protocol.getDevSerial(), protocol.getData()).buildObject();
                MeterData mdLeakage = HbTransformUtil.transform(leakage);
                meterDataList.add(mdLeakage);
                break;
            case CP_UPLOAD:
                CathodeProtection cp = new CathodeProtectionDecode(protocol.getDevSerial(), protocol.getData()).buildObject();
                MeterData mdCp = HbTransformUtil.transform(cp);
                meterDataList.add(mdCp);
                break;
            case ALARM_UPLOAD:
                Alertor alertor = new AlertorDecode(protocol.getDevSerial(), protocol.getData()).buildObject();
                meterDataList = HbTransformUtil.transform(alertor);
                break;
            default:
                return "不支持数据转换";
        }
        return getJson(meterDataList);
    }
}
